import React from "react";
import { Navigate } from "react-router-dom";

const PermissionGuard = ({ children, requiredPermissions = [] }) => {
  const userData = JSON.parse(localStorage.getItem("userData") || "{}");
  const userPermissions = userData.permissions || [];

  // 检查用户是否有所需的所有权限
  const hasRequiredPermissions = requiredPermissions.every((permission) =>
    userPermissions.includes(permission)
  );

  if (!hasRequiredPermissions) {
    return <Navigate to="/unauthorized" replace />;
  }

  return children;
};

export default PermissionGuard;
